package nz.co.doherty.gwtpbreadcrumbs.client.ui.topbar;

import java.util.ArrayList;

import nz.co.doherty.gwtpbreadcrumbs.client.ui.topbar.TopBarPresenter.Breadcrumb;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.InlineHyperlink;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.philbeaudoin.gwtp.mvp.client.ViewImpl;

public class TopBarViewImpl extends ViewImpl implements TopBarView {

	interface Binder extends UiBinder<Widget, TopBarViewImpl> {
	}

	private static Binder binder = GWT.create(Binder.class);

	private final Widget widget;

	@UiField
	Label title;
	
	@UiField
	FlowPanel breadCrumbsPanel;

	@Inject
	public TopBarViewImpl() {
		widget = binder.createAndBindUi(this);
	}

	@Override
	public Widget asWidget() {
		return widget;
	}

	@Override
	public void setBreadcrumbs(ArrayList<Breadcrumb> breadcrumbs) {
		this.breadCrumbsPanel.clear();
		for (int i = 0; i < breadcrumbs.size(); i++) {			
			if (i > 0) {
				this.breadCrumbsPanel.add(new InlineLabel(" > "));
			}
			Breadcrumb crumb = breadcrumbs.get(i);		
			this.breadCrumbsPanel.add(new InlineHyperlink(crumb.getTitle(), crumb.getToken()));
		}
	}

	@Override
	public void setTitle(String title) {
		this.title.setText(title);
	}
}
